/*
 * Manager.java
 *
 * Created on September 5, 2008, 9:30 PM
 */

package org.iwidget.desktop.manager;
import org.iwidget.desktop.core.*;
import org.iwidget.desktop.model.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author  Hakim
 */
public class Manager extends javax.swing.JFrame implements ActionListener{
    
    /** Creates new form Manager */
    public Manager(Iwidget iwidget) {
        initComponents();
        parent = iwidget;        
    }
    
    public JPanel getWidgetPanel(){
        return widgetPanel;
    }
    
    public void ConfigureButton(Hashtable wgtlist){
        widgetlist = wgtlist;
        
        int numpage  = (int)Math.ceil(wgtlist.size()/10);
        int start = curpage*10 + 1;
        int stop = start + 9;
        if (stop > widgetlist.size() && curpage > 0){
            stop = widgetlist.size() - start;
        }
        if (widgetlist.size() < 10)
            stop = widgetlist.size();
        
        String[] widgetname = new String[widgetlist.size()];
        int z = 0;
        for (Enumeration<String> e = widgetlist.keys(); e.hasMoreElements();)
        {
            widgetname[z++] = e.nextElement();
        }
        
        for (int i = start; i <= stop; i++){
            JButton btn = (JButton)widgetPanel.getComponent(i - start);
            btn.setName(widgetname[i - start]);
            btn.setText(widgetname[i - start]);
            //btn.setToolTipText(widgetname[i - start]);
            menu = new JPopupMenu();
            JMenuItem itemadd  = new JMenuItem("Add "+widgetname[i-start]);
            itemadd.setActionCommand(":"+widgetname[i-start]);
            itemadd.addActionListener(this);
            JMenuItem itemrem  = new JMenuItem("Remove "+widgetname[i-start]);
            itemrem.setActionCommand("rem"+widgetname[i-start]);
            itemrem.addActionListener(this);
            menu.add(itemadd);
            menu.add(itemrem);
            btn.setComponentPopupMenu(menu);
            
            WidgetElement widget = (WidgetElement) widgetlist.get(widgetname[i-start]);
            
            try{
                ElementIcon iconelement = widget.getIconElement();
                ImageIcon icon = iconelement.getImageData();
            if (icon != null) 
                btn.setIcon(icon);
            // else do nothing, no icon
            }catch(Exception e){
            }
            
            if (widget.getUIComponent() != null){
                btn.setEnabled(true);
            }else{
                btn.setEnabled(false);
            }
            
            //btnlist.put(widgetname[i-start], btn);
        }
    }
   
    private JButton getButtonByName(String name){
        int j = widgetPanel.getComponents().length;
        for (int i = 0; i < j; i++){
            JButton btn = (JButton)widgetPanel.getComponent(i);
            if (btn.getName().equals(name)){
                return btn;
            }
        }
        
        return null;
    }
    
    public void actionPerformed(ActionEvent e){
        if (e.getActionCommand().startsWith(":")){
            //String widgetname = e.getActionCommand().substring(1);
            parent.actionPerformed(e);
            // belum bisa add di buttonlist
            //JButton btn = (JButton)btnlist.get(widgetname);
            //btn.setEnabled(false);
        }else{
            String widgetname = e.getActionCommand().substring(3);
            parent.closeWidget(widgetname);
            
            // disable button
            //JButton btn = (JButton)btnlist.get(widgetname);
            //btn.setEnabled(false);
            //btnlist.remove(widgetname);
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        widgetPanel = new javax.swing.JPanel();
        widget01 = new javax.swing.JButton();
        widget03 = new javax.swing.JButton();
        widget04 = new javax.swing.JButton();
        widget07 = new javax.swing.JButton();
        widget02 = new javax.swing.JButton();
        widget05 = new javax.swing.JButton();
        widget06 = new javax.swing.JButton();
        widget08 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setTitle("iWidget widget Manager");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        widgetPanel.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(102, 102, 102)));

        widget01.setText("iWidget");
        widget01.setEnabled(false);
        widget01.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget01.setName("widget01"); // NOI18N
        widget01.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget03.setText("iWidget");
        widget03.setEnabled(false);
        widget03.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget03.setMaximumSize(new java.awt.Dimension(97, 91));
        widget03.setMinimumSize(new java.awt.Dimension(97, 91));
        widget03.setName("widget03"); // NOI18N
        widget03.setPreferredSize(new java.awt.Dimension(97, 91));
        widget03.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget04.setText("iWidget");
        widget04.setEnabled(false);
        widget04.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget04.setName("widget04"); // NOI18N
        widget04.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget07.setText("iWidget");
        widget07.setEnabled(false);
        widget07.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget07.setName("widget07"); // NOI18N
        widget07.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget02.setText("iWidget");
        widget02.setEnabled(false);
        widget02.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget02.setName("widget02"); // NOI18N
        widget02.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget05.setText("iWidget");
        widget05.setEnabled(false);
        widget05.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget05.setName("widget05"); // NOI18N
        widget05.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget06.setText("iWidget");
        widget06.setEnabled(false);
        widget06.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget06.setName("widget06"); // NOI18N
        widget06.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        widget08.setText("iWidget");
        widget08.setEnabled(false);
        widget08.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        widget08.setName("widget08"); // NOI18N
        widget08.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout widgetPanelLayout = new javax.swing.GroupLayout(widgetPanel);
        widgetPanel.setLayout(widgetPanelLayout);
        widgetPanelLayout.setHorizontalGroup(
            widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(widgetPanelLayout.createSequentialGroup()
                .addGroup(widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(widgetPanelLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(widget01, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                    .addGroup(widgetPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(widget03, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addGroup(widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(widget02, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget04, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(widget06, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget08, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(widget07, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(widget05, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );
        widgetPanelLayout.setVerticalGroup(
            widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(widgetPanelLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(widget01, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(widget02, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget06, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget07, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget05, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addGroup(widgetPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(widget03, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget04, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widget08, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(72, 72, 72))
        );

        jButton1.setText(">>");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("<<");

        jButton3.setText("All");

        jButton4.setText("Recommendation");

        jButton5.setText("News");

        jButton6.setText("Sport");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(569, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(107, Short.MAX_VALUE))
            .addComponent(widgetPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton3)
                    .addComponent(jButton5)
                    .addComponent(jButton6))
                .addGap(18, 18, 18)
                .addComponent(widgetPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               Manager c =  new Manager(null);
               JPanel panel = c.getWidgetPanel();
               for (int i=0; i < panel.getComponents().length; i++){
                   System.out.println(panel.getComponent(i).getName());
               }
               c.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton widget01;
    private javax.swing.JButton widget02;
    private javax.swing.JButton widget03;
    private javax.swing.JButton widget04;
    private javax.swing.JButton widget05;
    private javax.swing.JButton widget06;
    private javax.swing.JButton widget07;
    private javax.swing.JButton widget08;
    private javax.swing.JPanel widgetPanel;
    // End of variables declaration//GEN-END:variables
    private Iwidget parent;
    private Hashtable widgetlist;
    private Hashtable btnlist;
    private JPopupMenu menu ;
    private int curpage = 0;    //current page
            
}
